package com.stylefeng.guns.modular.netcar.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.Tools.CodeMsg;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.core.validater.Insert;
import com.stylefeng.guns.core.validater.Update;
import com.stylefeng.guns.modular.netcar.model.TaskReleaseReason;
import com.stylefeng.guns.modular.netcar.service.ITaskReleaseReasonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 驾驶员拒绝任务或释放任务原因字典
 *
 * @author sasazaixian
 * @Date 2018-10-23 11:04
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/taskReleaseReason"})
@Api(value = "驾驶员拒绝任务或释放任务原因字典操作相关api", tags = {"驾驶员拒绝任务或释放任务原因字典操作接口"})
@Slf4j(topic = "驾驶员拒绝任务或释放任务原因字典")
public class TaskReleaseReasonController extends BaseController {
    @Autowired
    private ITaskReleaseReasonService taskReleaseReasonService;
	
    /**
     * 分页获取驾驶员拒绝任务或释放任务原因字典列表
     * @param taskReleaseReason
     * @param pageNum
     * @param pageSize
     * @return	
     */
    @ApiOperation(value = "分页获取驾驶员拒绝任务或释放任务原因字典列表", notes = "分页获取驾驶员拒绝任务或释放任务原因字典列表", httpMethod = "POST")
    @PostMapping(value = "/pageList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)	
    @ResponseBody
    public Result pageList(
        @RequestBody @ApiParam(name = "taskReleaseReason", value = "驾驶员拒绝任务或释放任务原因字典对象")TaskReleaseReason taskReleaseReason,
        @ApiParam(name = "pageNum", value = "当前页数", required = true) @RequestParam(value = "pageNum", required = true) Integer pageNum,
        @ApiParam(name = "pageSize", value = "每页显示条数", required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize		
        ) {
        try {
            Pager pager=new Pager();
            pager.setPageNumber(pageNum);//页码从1 开始
            pager.setPageSize(pageSize);
            pager.setObj(taskReleaseReason);			
            return Result.success( taskReleaseReasonService.pageListTaskReleaseReason(pager,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取驾驶员拒绝任务或释放任务原因字典列表
     * @param taskReleaseReason    
     * @return	
     */
    @ApiOperation(value = "获取驾驶员拒绝任务或释放任务原因字典列表", notes = "获取驾驶员拒绝任务或释放任务原因字典列表", httpMethod = "POST")
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)	
    @ResponseBody
    public Result list(
        @RequestBody @ApiParam(name = "taskReleaseReason", value = "驾驶员拒绝任务或释放任务原因字典对象")TaskReleaseReason taskReleaseReason) {
        try {            
            return Result.success( taskReleaseReasonService.selectListTaskReleaseReason(taskReleaseReason,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 通过主键获取驾驶员拒绝任务或释放任务原因字典对象
     * @param id
     * @return
     */
    @ApiOperation(value = "通过主键获取驾驶员拒绝任务或释放任务原因字典对象", notes = "通过主键获取驾驶员拒绝任务或释放任务原因字典对象", httpMethod = "GET")
    @GetMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result getById(
            @ApiParam(name = "id", value = "主键ID", required = true) @RequestParam(value = "id", required = true) String  id
    ) {
        try {
            return Result.success( taskReleaseReasonService.findTaskReleaseReasonById(id,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 新增驾驶员拒绝任务或释放任务原因字典数据
     *
     * @param taskReleaseReason 
     * @param result
     * @return
     */
    @ApiOperation(value = "新增驾驶员拒绝任务或释放任务原因字典数据", notes = "新增驾驶员拒绝任务或释放任务原因字典数据", httpMethod = "POST")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "新增驾驶员拒绝任务或释放任务原因字典数据")
    @ResponseBody
    public Result add( @Validated(value = Insert.class) @RequestBody @ApiParam(name = "taskReleaseReason ", value = "驾驶员拒绝任务或释放任务原因字典对象")TaskReleaseReason taskReleaseReason ,BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            taskReleaseReasonService.insertTaskReleaseReason(taskReleaseReason,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }


    /**
     * 更新驾驶员拒绝任务或释放任务原因字典数据
     * @param taskReleaseReason
     * @param result
     * @return
     */
    @ApiOperation(value = "更新驾驶员拒绝任务或释放任务原因字典数据", notes = "更新驾驶员拒绝任务或释放任务原因字典数据", httpMethod = "POST")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "更新驾驶员拒绝任务或释放任务原因字典数据")
    @ResponseBody
    public Result update(@Validated(value = Update.class) @RequestBody @ApiParam(name = "taskReleaseReason", value = "驾驶员拒绝任务或释放任务原因字典对象")TaskReleaseReason taskReleaseReason, BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            taskReleaseReasonService.updateTaskReleaseReason(taskReleaseReason,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 删除驾驶员拒绝任务或释放任务原因字典数据
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除驾驶员拒绝任务或释放任务原因字典数据", notes = "删除驾驶员拒绝任务或释放任务原因字典数据", httpMethod = "GET")
    @GetMapping(value = "/delBatch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "删除驾驶员拒绝任务或释放任务原因字典数据")
    @ResponseBody
    public Result delBatch(
            @ApiParam(name = "ids", value = "主键ID字符数组", required = true) @RequestParam(value = "ids", required = true) String ids
    ) {
        try {
            taskReleaseReasonService.deleteBatchTaskReleaseReason(ids.split(","),getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
	/**
     * 删除驾驶员拒绝任务或释放任务原因字典数据
     * @param taskReleaseReason
     * @return
     */
    @ApiOperation(value = "删除驾驶员拒绝任务或释放任务原因字典数据", notes = "删除驾驶员拒绝任务或释放任务原因字典数据", httpMethod = "POST")
    @PostMapping(value = "/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "删除驾驶员拒绝任务或释放任务原因字典数据")
    @ResponseBody
    public Result del(@RequestBody @ApiParam(name = "taskReleaseReason", value = "驾驶员拒绝任务或释放任务原因字典对象")TaskReleaseReason taskReleaseReason) {
        try {
            taskReleaseReasonService.deleteTaskReleaseReason(taskReleaseReason,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取驾驶员拒绝任务或释放任务原因字典的Key-Value列表，用于html的Select数据源
     * @param taskReleaseReason
     * @return
     */
    @ApiOperation(value = "获取驾驶员拒绝任务或释放任务原因字典的Key-Value列表，用于html的Select数据源", notes = "获取驾驶员拒绝任务或释放任务原因字典的Key-Value列表，用于html的Select数据源", httpMethod = "POST")
    @PostMapping(value = "/optionsList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result optionsList(@RequestBody @ApiParam(name = "taskReleaseReason", value = "驾驶员拒绝任务或释放任务原因字典对象")TaskReleaseReason taskReleaseReason) {
        try {
            return Result.success(taskReleaseReasonService.optionsTaskReleaseReason(taskReleaseReason,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
	private String getToken(){		
		return getHttpServletRequest().getHeader("Authorization").substring(7);
    }
}
